package sitonya.entity.system

import com.icecreamqaq.yudb.jpa.JPADao
import onedice.utils.ToPlatFormId.ToPlatformId.toPlatformId
import org.hibernate.Hibernate
import org.hibernate.annotations.CacheConcurrencyStrategy
import sitonya.concat.BOT_ID
import sitonya.concat.PLATFORM_BOT_ID
import sitonya.concat.PLATFORM_KEY
import javax.persistence.*
import javax.validation.constraints.NotNull

interface ISwitch : JPADao<Switch, Long> {
    fun findById(
        groupId: Long = 0,
        botId: Long = 0
    ): Switch? {
        val platformGroupId = toPlatformId(groupId, PLATFORM_KEY)
        return search("from Switch where platformGroupId=?0 and platformBotId=?1", platformGroupId, PLATFORM_BOT_ID)
    }
}

@Entity
@Table(
    name = "Switch",
    catalog = "diceSystem",
    uniqueConstraints = [UniqueConstraint(name="indexSwitch", columnNames = ["platformBotId","platformGroupId"])]
)
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "system")
data class Switch(
    @NotNull
    @Column(name = "botId")
    var botId: Long = BOT_ID,

    @NotNull
    @Column(name = "platformBotId")
    var platformBotId: String = PLATFORM_BOT_ID,

    @NotNull
    @Column(name = "groupId")
    var groupId: Long = 0L,

    @NotNull
    @Column(name = "platformGroupId")
    var platformGroupId: String = toPlatformId(groupId, PLATFORM_KEY),

    @NotNull
    @Column(name = "enabled")
    var enabled: Boolean = true,

    @Id
    @GeneratedValue
    @Column(name = "oid")
    var oid: Long? = null
) {
    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other == null || Hibernate.getClass(this) != Hibernate.getClass(other)) return false
        other as Switch

        return oid != null && oid == other.oid
    }

    override fun hashCode(): Int = javaClass.hashCode()

    @Override
    override fun toString(): String {
        return this::class.simpleName + "(platformBotId = $platformBotId , platformGroupId = $platformGroupId )"
    }
}


